// pages/detail/detail.js
const db = wx.cloud.database()
const list = db.collection('list')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        states: [
            '已完成', '未完成'
        ],
        state: '',
        task: '',
        discription: '',
        date: '',
        _id: '',
        _openid: '',
        show: false,
        showPop: false
    },

    /**
     * 事件状态关闭弹出层
     */
    cancelPick() {
        this.setData({
            showPop: false
        })
    },

    /**
     * 确认更改事件状态
     */
    confirmSelection(e) {
        this.setData({
            showPop: false,
            state: e.detail.value
        })
    },

    /**
     * 关闭事件状态弹出层
     */
    closePop() {
        this.setData({
            showPop: false
        })
    },

    /**
     * 打开事件状态弹出层
     */
    openPop() {
        this.setData({
            showPop: true
        })
    },

    /**
     * 改变日期
     */
    formatDate(date) {
        date = new Date(date);
        return `${date.getMonth() + 1}/${date.getDate()}`;
    },
    onConfirm(event) {
        this.setData({
            show: false,
            date: this.formatDate(event.detail),
        });
    },

    /**
     * 关闭弹出层
     */
    onClose() {
        this.setData({
            show: false
        })
    },

    /**
     * 弹出日历层
     */
    onDisplay() {
        this.setData({
            show: true
        })
    },

    /**
     * 更新数据库
     */
    update() {
        var state = this.data.state
        var task = this.data.task
        var discription = this.data.discription
        var date = this.data.date
        var _id = this.data._id
        var _openid = this.data._openid
        if (this.data.task === '') {
            wx.showToast({
                title: '待办事项必填哦',
                icon: 'error'
            })
            return;
        }
        if (this.data.state === '') {
            wx.showToast({
                title: '事件状态必填哦',
                icon: 'error'
            })
            return;
        }
        list.doc(this.data._id).set({
            data: {
                state: state,
                task: task,
                discription: discription,
                date: date
            },
            success: function (res) {

                var pages = getCurrentPages()
                var prePage = pages[pages.length - 2]

                prePage.setData({
                    update_task: {
                        state: state,
                        task: task,
                        discription: discription,
                        date: date,
                        _id: _id,
                        _openid: _openid
                    }
                })

                wx.navigateBack({
                    delta: 1,
                })
            }
        })
    },

    /**
     * 删除该记录
     */
    delete() {
        var _id = this.data._id
        var state = this.data.state
        list.doc(this.data._id).remove({
            success: function () {

                var pages = getCurrentPages()
                var prePage = pages[pages.length - 2]

                prePage.setData({
                    delete_id: _id,
                    delete_state: state
                })

                wx.navigateBack({
                    delta: 1
                })
            }
        })
    },

    /**
     * 更新事项名称
     */
    changeTask(e) {
        this.setData({
            task: e.detail
        })
    },

    /**
     * 更新事项详情
     */
    changeDiscription(e) {
        this.setData({
            discription: e.detail
        })
    },

    /**
     * 更新日期
     */
    changeDate(e) {
        this.setData({
            date: e.detail
        })
    },

    /**
     * 更新完成状态
     */
    changeState(e) {
        this.setData({
            state: e.detail
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            _id: options._id,
            _openid: options._openid,
            task: options.task,
            date: options.date,
            state: options.state,
            discription: options.discription
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})